﻿using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace VIT.Provider
{
    public class TemplateConfig : TemplateConfig_Base
    {
        #region Variable
        string user = HttpContext.Current.Session["userName"].ToString();
        #endregion

        #region Constructor
        public TemplateConfig()
        {
            this.LanguageId = string.Empty;            
            this.TemplateName = string.Empty;
            this.TemplateConfigBy = string.Empty;
            this.FromDate = 0;
            this.ToDate = 0;
        }
        #endregion

        #region Methods
        public DataTable GetAll(string SortColumn = "", bool isTransaction = false)
        {
            if (SortColumn == "") SortColumn = "order by LanguageId, t.ModifyDate Desc";
            else SortColumn = "order by " + SortColumn;
            string strQuery = "select t.Id, t.LanguageId, TemplateName, TemplateConfigBy, FromDate, ToDate, t.ModifyDate, t.ModifyByUser from TemplateConfig t, WebConfig w where t.LanguageId=w.LanguageId " + SortColumn;
            return ExecuteDataset(isTransaction, strQuery).Tables[0];
        }

        public bool Delete(int id, bool isTransaction = false)
        {
            string strQuery = "Delete from TemplateConfig where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);

            return ExecuteNonQuery(isTransaction, strQuery, Id) > 0;
        }

        public void GetSingle(bool isTransaction = false)
        {
            if (Exist())
            {
                string strQuery = "select t.Id, t.LanguageId, TemplateName, TemplateConfigBy, FromDate, ToDate from TemplateConfig t, WebConfig w where t.LanguageId=w.LanguageId and t.Id=@Id";
                SqlParameter Id = new SqlParameter("@Id", this.Id);
                DataTable dt = ExecuteDataset(isTransaction, strQuery, Id).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    this.Id = Convert.ToInt32(dt.Rows[0]["Id"]);
                    this.LanguageId = dt.Rows[0]["LanguageId"].ToString();
                    this.TemplateName = dt.Rows[0]["TemplateName"].ToString();
                    this.TemplateConfigBy = dt.Rows[0]["TemplateConfigBy"].ToString();
                    this.FromDate = Convert.ToInt32(dt.Rows[0]["FromDate"]);
                    this.ToDate = Convert.ToInt32(dt.Rows[0]["ToDate"]);
                }
            }
            else this.Id = -1;
        }

        public bool Exist(bool isTransaction = false)
        {
            if (this.Id < 0) return false;
            string strQuery = "select Id from TemplateConfig where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            return ExecuteScalar(isTransaction, strQuery, Id) != null;
        }

        public bool Save(bool isTransaction = true)
        {
            TransactionBegin();
            try
            {
                if (Exist(isTransaction)) Update(isTransaction);
                else Insert(isTransaction);

                SetConfigType(isTransaction);

                TransactionCommit();
                return true;
            }
            catch
            {
                TransactionRollback();
                return false;
            }
            finally
            {
                HttpContext.Current.Application["ModuleData"] = null;
            }
        }

        public int Insert(bool isTransaction = false)
        {
            string strQuery = "insert into TemplateConfig(LanguageId, TemplateName, FromDate, ToDate, CreateDate, ModifyDate, CreateByUser, ModifyByUser) values (@LanguageId, @TemplateName, @FromDate, @ToDate, @Now, @Now, @User, @User)";

            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            SqlParameter TemplateName = new SqlParameter("@TemplateName", this.TemplateName);
            SqlParameter FromDate = new SqlParameter("@FromDate", this.FromDate);
            SqlParameter ToDate = new SqlParameter("@ToDate", this.ToDate);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(isTransaction, strQuery, LanguageId, TemplateName, FromDate, ToDate, User, Now);
        }

        public int Update(bool isTransaction = false)
        {
            string strQuery = "update TemplateConfig set LanguageId=@LanguageId, TemplateName=@TemplateName, FromDate=@FromDate, ToDate=@ToDate, ModifyDate=@Now, ModifyByUser=@User where Id=@Id";

            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            SqlParameter TemplateName = new SqlParameter("@TemplateName", this.TemplateName);
            SqlParameter FromDate = new SqlParameter("@FromDate", this.FromDate);
            SqlParameter ToDate = new SqlParameter("@ToDate", this.ToDate);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(isTransaction, strQuery, Id, LanguageId, TemplateName, FromDate, ToDate, User, Now);
        }

        public bool SetConfigType(bool isTransaction = false)
        {
            string strQuery = "update WebConfig set TemplateConfigBy=@TemplateConfigBy where LanguageId=@LanguageId";

            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            SqlParameter TemplateConfigBy = new SqlParameter("@TemplateConfigBy", this.TemplateConfigBy);

            return ExecuteNonQuery(isTransaction, strQuery, LanguageId, TemplateConfigBy) > 0;
        }
        #endregion
    }
}
